/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.StafferBean;
import com.minierp.dao.StafferDao;
import com.minierp.model.Staffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class StafferValidator implements Validator {
	private StafferDao stafferDao;

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}

	public boolean supports(Class clazz) {
		return StafferBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		StafferBean form = (StafferBean) obj;
		boolean hasError = false;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Staffer e;
		if (form.getCodeRule().getGenerateRule().equals("MANUAL")
				|| form.getStaffer().getId() != 0L) {
			if (form.getStaffer().getStafferCode() != null
					&& !form.getStaffer().getStafferCode().trim().equals("")) {
				e = this.stafferDao.get(form.getStaffer().getStafferCode());
				if (e != null && e.getId() != form.getStaffer().getId()) {
					errors.rejectValue("staffer.stafferCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("staffer.stafferCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			if (form.getStaffer().getStafferName() != null) {
				form.getStaffer().setStafferName(
						form.getStaffer().getStafferName().trim());
			}

			if (form.getStaffer().getStafferName() != null
					&& !form.getStaffer().getStafferName().equals("")) {
				e = this.stafferDao.getByName(form.getStaffer()
						.getStafferName());
				if (e != null && e.getId() != form.getStaffer().getId()) {
					errors.rejectValue("staffer.stafferName", "", "『姓名』已经存在");
					hasError = true;
				}
			} else {
				hasError = true;
				errors.rejectValue("staffer.stafferName", "", "请输入『姓名』");
			}
		}

		if (!hasError) {
			if (form.getBirthdayStr() != null
					&& !form.getBirthdayStr().equals("")) {
				try {
					form.getStaffer().setBirthday(
							sf.parse(form.getBirthdayStr()));
				} catch (Exception arg8) {
					hasError = true;
					errors.rejectValue("birthdayStr", "",
							"『生日』填写错误，请按以下格式输入：2008-01-01");
				}
			} else {
				form.getStaffer().setBirthday((Date) null);
			}
		}

		if (!hasError && form.getStaffer().getDepartmentId() == 0L) {
			hasError = true;
			errors.rejectValue("staffer.departmentId", "", "请选择『部门』");
		}

		if (!hasError) {
			if (form.getInDateStr() != null && !form.getInDateStr().equals("")) {
				try {
					form.getStaffer().setInDate(sf.parse(form.getInDateStr()));
				} catch (Exception arg7) {
					hasError = true;
					errors.rejectValue("inDateStr", "",
							"『入职日期』填写错误，请按以下格式输入：2008-01-01");
				}
			} else {
				form.getStaffer().setInDate((Date) null);
			}
		}

		if (!hasError && form.getStaffer().getSuperiorId() != 0L
				&& form.getStaffer().getId() != 0L) {
			if (form.getStaffer().getSuperiorId() == form.getStaffer().getId()) {
				hasError = true;
				errors.rejectValue("superiorName", "", "不能选择自己作为『上级』");
			} else {
				e = (Staffer) this.stafferDao.get(Staffer.class, form
						.getStaffer().getSuperiorId());
				if (e != null
						&& e.getSuperiorIdStr() != null
						&& e.getSuperiorIdStr().indexOf(
								";" + form.getStaffer().getId() + ";") != -1) {
					hasError = true;
					errors.rejectValue("superiorName", "", "不能选择自己的下级作为『上级』");
				}
			}
		}

		if (!hasError) {
			if (form.getStaffer().getEnabled().equals("Y")) {
				form.getStaffer().setLeaveDate((Date) null);
			} else if (form.getLeaveDateStr() != null
					&& !form.getLeaveDateStr().equals("")) {
				try {
					form.getStaffer().setLeaveDate(
							sf.parse(form.getLeaveDateStr()));
				} catch (Exception arg6) {
					hasError = true;
					errors.rejectValue("leaveDateStr", "",
							"『离职日期』填写错误，请按以下格式输入：2008-01-01");
				}
			} else {
				form.getStaffer().setLeaveDate((Date) null);
			}
		}

		if (!hasError && form.getStaffer().getMark() != null
				&& form.getStaffer().getMark().length() > 100) {
			errors.rejectValue("staffer.mark", "", "『备注』不能超过100个字符");
		}

	}
}